package eu.chargetime.ocpp.jsonserverimplementation.controller;

import cn.hutool.json.JSONUtil;
import eu.chargetime.ocpp.jsonserverimplementation.server.ISessionStoreService;
import eu.chargetime.ocpp.model.Confirmation;
import eu.chargetime.ocpp.model.core.RemoteStartTransactionConfirmation;
import eu.chargetime.ocpp.model.core.RemoteStopTransactionConfirmation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author zhaorui
 * @Date 2024/4/21  15:05
 * @description:
 */
@RestController
@Slf4j
public class ChargeController {

    @Resource
    private ISessionStoreService sessionStoreService;


    /**
     * 开始充电
     *
     * @param sessionId
     * @param idTag
     * @return
     */
    @GetMapping("/startCharge")
    public String startCharge(@RequestParam String sessionId,@RequestParam String idTag) {
        log.info("[startCharge]方法被执行了,sessionId:{}",sessionId);
        Confirmation confirmation = sessionStoreService.startMessage(sessionId, idTag);
        if(Objects.nonNull(confirmation)){
            RemoteStartTransactionConfirmation result = (RemoteStartTransactionConfirmation) confirmation;
            log.info("[startCharge] 返回值,result:{}", JSONUtil.toJsonStr(result));
            return JSONUtil.toJsonStr(result);
        }
        String a = "test";
        System.out.println(a);
        return a;
    }

    /**
     * 结束充电
     *
     * @param sessionId
     * @return
     */
    @GetMapping("/stopCharge")
    public String stopCharge(@RequestParam String sessionId,@RequestParam Integer transactionId ) {
        log.info("[test]方法被执行了,sessionId:{}",sessionId);
        Confirmation confirmation = sessionStoreService.stopMessage(sessionId, transactionId);
        if(Objects.nonNull(confirmation)){
            RemoteStopTransactionConfirmation result = (RemoteStopTransactionConfirmation) confirmation;
            log.info("[stopCharge] 返回值,result:{}", JSONUtil.toJsonStr(result));
            return JSONUtil.toJsonStr(result);
        }
        String a = "test";
        System.out.println(a);
        return a;
    }

}
